package com.harmontronics.hanma.paas.paas.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Pattern;

/**
 * 域名解析工具类
 */
@Slf4j
public class DomainUtils {
    private static Pattern IP_PATTERN = Pattern.compile("(\\d{1,3}\\.){3}(\\d{1,3})");


    /**
     * 获取url的域名后缀
     * @param url  格式：http://news-Suffix.yy.com/2017-09-23/190978248.html
     * @return
     */
    public static String getDomainSuffix(String url){
        try{
//            if (url.endsWith(".")){
//                url = url.substring(0, url.length() - 1);
//            }
            if (IP_PATTERN.matcher(url).matches()){
                return "";
            }
            int index = url.indexOf('.');
            //获得http://news-Suffix，并且去空格
            String subUrl = url.substring(0,index).replace(" ", "");
            index = subUrl.indexOf("//");
            if(index<=0) {
                return "";
            }
            //获得news-Suffix
            subUrl = subUrl.substring(index+2);
            index = subUrl.indexOf("-");
            //获得Suffix
            if(index>0){
                return subUrl.substring(index+1);
            }
        }catch (Exception e){
            log.error(e.getMessage(), e);
        }
        return "";
    }

}
